
namespace chatroom{

    class UserTable
    {
        public:
            using Ptr = std::shared_ptr<UserTable>;
            UserTable(std::shared_ptr<odb::core::database> db):_db(db){}
            bool insert(std::shared_ptr<chatroom::User>& user){
                try{
                    //开启事务
                    odb::transaction t(_db->begin());
                    _db->persist(*user);
                    t.commit();
                }catch(std::exception& e){
                    Logger_ERROR("{}新增用户失败",user->get_user_id());
                    return false;
                }
                return true;
            }
            bool update(std::shared_ptr<chatroom::User>& user){
                try{
                    //开启事务
                    odb::transaction t(_db->begin());
                    _db->update(*user);
                    t.commit();
                }catch(std::exception& e){
                    Logger_ERROR("{}更新用户失败",user->get_user_id());
                    return false;
                }
                return true;
            }
            std::shared_ptr<User> select_by_userid(const std::string& user_id)
            {
                std::shared_ptr<User> res;
                try{
                    //开启事务
                    odb::transaction t(_db->begin());
                    typedef odb::query<chatroom::User> query;
                    typedef odb::result<chatroom::User> result;
                    res.reset(_db->query_one<chatroom::User>(query::user_id == user_id));
                    t.commit();
                }catch(std::exception& e){
                    Logger_ERROR("通过用户ID查询用户失败",user_id,e.waht());
                    return res;
                }
                return res;
            }
            std::shared_ptr<User> select_by_phone(const std::string& phone)
            {
                std::shared_ptr<User> res;
                try{
                    //开启事务
                    odb::transaction t(_db->begin());
                    typedef odb::query<chatroom::User> query;
                    typedef odb::result<chatroom::User> result;
                    res.reset(_db->query_one<chatroom::User>(query::phone == phone));
                    t.commit();
                }catch(std::exception& e){
                    Logger_ERROR("通过手机号查询用户失败{}-{}",phone,e.what());
                    return res;
                }
                return res;
            }
            std::shared_ptr<User> select_by_nickname(const std::string& nickname)
            {
                std::shared_ptr<User> res;
                try{
                    //开启事务
                    odb::transaction t(_db->begin());
                    typedef odb::query<chatroom::User> query;
                    typedef odb::result<chatroom::User> result;
                    res.reset(_db->query_one<chatroom::User>(query::nickname == nickname));
                    t.commit();
                }catch(std::exception& e){
                    Logger_ERROR("通过昵称查询用户失败",nickname,e.what());
                    return res;
                }
                return res;
            }
            std::vector<chatroom::User> slelect_by_idlist(const std::vector<string>& idlist)
            {
                std::vector<chatroom::User> res;
                if(idlist.empty())
                {
                    Logger_ERROR("批量用户传入的信息是空");
                    return res;
                }
                try{
                    //开启事务
                    odb::transaction t(_db->begin());
                    typedef odb::query<chatroom::User> query;
                    typedef odb::result<chatroom::User> result;
                    stringstream ss;
                    ss<<"user_id in (";
                    for(auto& id:idlist)
                    {
                        ss<<"'"<<id<<"',";
                    }
                    std::string condition = ss.str();
                    condition.pop_back();
                    condition+=")";
                    result r(_db->query<chatroom::User>(condition));
                    for(auto i(r.begin()); i != r.end(); ++i)
                    {
                        res.push_back(*i);
                    }
                    t.commit();
                }catch(std::exception& e){
                    Logger_ERROR("批量查询用户失败{}",e.what());
                    return res;
                }
                return res;
            }
        private:
            std::shared_ptr<odb::core::database> _db
    }
}